package flash.display;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

/**
 * @author Joa Ebert
 */
public abstract class DisplayObjectContainer extends InteractiveObject {
	private final List<DisplayObject> _children = new LinkedList<DisplayObject>();

	public int numChildren() {
		return _children.size();
	}

	public DisplayObject addChildAt(final int index, final DisplayObject child) {
		_children.add(index, child);
		return child;
	}

	public DisplayObject addChild(final DisplayObject child) {
		return addChildAt(_children.size(), child);
	}
	
	public DisplayObject removeChildAt(final int index) {
		return _children.remove(index);
	}
	
	public DisplayObject removeChild(final DisplayObject child) {
		return removeChildAt(getChildIndex(child));
	}
	
	public boolean contains(final DisplayObject child) {
		return _children.contains(child);
	}
	
	public int getChildIndex(final DisplayObject child) {
		return _children.indexOf(child);
	}
	
	public void setChildIndex(final DisplayObject child, final int index) {
		final int childIndex = getChildIndex(child);
		Collections.swap(_children, childIndex, index);
	}
	
	public void swapChildren(final DisplayObject child1, final DisplayObject child2) {
		final int index1 = getChildIndex(child1);
		final int index2 = getChildIndex(child2);
		Collections.swap(_children, index1, index2);
	}
	
	public void swapChildrenAt(final int index1, final int index2) {
		Collections.swap(_children, index1, index2);
	}
	
	protected final void JITB$renderChildren() {
		for(final DisplayObject child : _children) {
			child.JITB$renderDisplayObject();
		}
	}
}
